<?php
 
namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\VerificationCodesRequest;
use App\Support\Sms\SmsCodesMakeSupport;//这个短信模板没有 继承 contracts 接口

use App\Contracts\Sms\SmsCacheContracts;
use App\Contracts\Sms\SmsContracts;//用来发送短信



class VerificationCodesController extends Controller
{
    public function store(VerificationCodesRequest $request,SmsCodesMakeSupport $codesMake,SmsContracts $smsSupport,SmsCacheContracts $SmsCacheSupport)
    {
        $phone = $request->input('phone');
        $code = $codesMake->VerificationMake();

        try{
            $result = "ok";
            // $smsSupport->send($code,$phone);
        }catch (\Exception $exception){
            return response()->json([
                "error" => $exception->getMessage()
            ]);
        }
        $key = 'sms::code::verification::'.$phone.'::'.str_pad(random_int(1,9999),4,0,STR_PAD_LEFT);
        $SmsCacheSupport->setExp(120);
        $SmsCacheSupport->VerificationCache($key, $codesMake->getCode(), $phone);

        return response()->json([
            'key' => $key,
            'expired_at' => $SmsCacheSupport->getExp()->toDateTimeString(),
            'result' => $result,
            'code' => $code
        ])->setStatusCode(200);
    }
}
 

